﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace VSM
{
    public partial class Available2PromisePopup : System.Web.UI.Page
    {
        private int _rowCount = 0;
        private int _checkRow = 0;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[Utils.AppConstants.ID_User] == null
                || Session[Utils.AppConstants.Password] == null
                || Session[Utils.AppConstants.ID_Dealer] == null)
            {
                Response.Redirect("Login.aspx");
            }
            if (!IsPostBack)
            {
                hdnItemCode.Value = Request.QueryString["ItemCode"];
                hdndbType.Value = Request.QueryString["dbType"];
                hdnOnHand.Value = GetVSMOnHandQty(hdnItemCode.Value);
                LoadAvailable2Promise();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAccept_Click(object sender, EventArgs e)
        {
            try
            {
                //Dictionary<string, string> dicItem = new Dictionary<string, string>();
                //for (int i = 0; i < this.grdItem.Rows.Count; i++)
                //{
                //    CheckBox checkBox = (CheckBox)this.grdItem.Rows[i].FindControl("chkChild");

                //    if (checkBox != null)
                //    {
                //        if (checkBox.Checked)
                //        {
                //            Label code = (Label)this.grdItem.Rows[i].FindControl("lblItemCode");
                //            Label Name = (Label)this.grdItem.Rows[i].FindControl("lblItemName");
                //            dicItem.Add(code.Text, Name.Text);
                //        }
                //    }
                //}
                //Session["ChosenItem"] = dicItem;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "OK", "Main.okDialogClick('SelectItem');", true);
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdAva_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");
                e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");
                e.Row.Attributes["style"] = "cursor:pointer";  
            }
        }

        private string GetVSMOnHandQty(string ItemCode)
        {
            // Get InStock Qty
            GeneralFunctions GF = new GeneralFunctions(Utils.AppConstants.VSM, Session[Utils.AppConstants.ID_Dealer].ToString());
            string lsSqlCommand = "select SUM(IsNull(OnHand, 0) ) As OnHand";
            lsSqlCommand += " From OITW where ItemCode = '" + ItemCode + "' and WhsCode In ('HCM-SAL', 'HN-SAL') ";
            return GF.GetSingleValue(Utils.AppConstants.VSM, lsSqlCommand, "OnHand");
        }

        private void LoadAvailable2Promise()
        {
            try
            {
                GeneralFunctions GF = new GeneralFunctions(Utils.AppConstants.VSM, Session[Utils.AppConstants.ID_Dealer].ToString());

                DataSet dsParamArrays = new DataSet();
                DataTable dtParamArrays = new DataTable();
                dtParamArrays.Columns.Add("key");
                dtParamArrays.Columns.Add("value");
                DataRow drPara = dtParamArrays.NewRow();
                drPara["key"] = "@ItemCode"; drPara["value"] = hdnItemCode.Value;
                dtParamArrays.Rows.Add(drPara);
                dsParamArrays.Tables.Add(dtParamArrays.Copy());
                // Call sp ...
                DataTable dtAvailable = null;
                if(hdndbType.Value.Equals(Utils.AppConstants.VSM))
                    dtAvailable = GF.GetSummaryOfInventory("sp_fun_GetVSMQtyPlanOfVSM", dsParamArrays, Utils.AppConstants.VSM);
                else
                    dtAvailable = GF.GetSummaryOfInventory("sp_fun_GetVSMQtyPlan", dsParamArrays, Utils.AppConstants.VSM);


                DataTable dt = new DataTable("vw_Available2Promise");
                dt.Columns.Add("No");
                dt.Columns.Add("WeekOfMonth");
                dt.Columns.Add("WhsName");
                dt.Columns.Add("Qty");

                if (dtAvailable != null && dtAvailable.Rows.Count > 0)
                {
                    DataRow row = dt.NewRow();
                    string lsOldWhseCode = dtAvailable.Rows[0]["WhsCode"].ToString();
                    int liPreQty = 0;

                    row["No"] = dtAvailable.Rows[0]["No"];
                    row["WeekOfMonth"] = dtAvailable.Rows[0]["WeekOfMonth"];
                    row["WhsName"] = dtAvailable.Rows[0]["WhsName"];
                    liPreQty = int.Parse(string.IsNullOrEmpty(dtAvailable.Rows[0]["Qty"].ToString()) ? "0" : dtAvailable.Rows[0]["Qty"].ToString());
                    if (liPreQty > 15 && !hdndbType.Value.Equals(Utils.AppConstants.VSM))
                        row["Qty"] = 15;
                    else
                        row["Qty"] = liPreQty;
                    dt.Rows.Add(row);

                    for(int i = 1; i < dtAvailable.Rows.Count; i++)
                    {
                        row = dt.NewRow();
                        row["No"] = dtAvailable.Rows[i]["No"];
                        row["WeekOfMonth"] = dtAvailable.Rows[i]["WeekOfMonth"];
                        row["WhsName"] = dtAvailable.Rows[i]["WhsName"];
                        if (!lsOldWhseCode.Equals(dtAvailable.Rows[i]["WhsCode"].ToString()))
                        {
                            lsOldWhseCode = dtAvailable.Rows[i]["WhsCode"].ToString();
                            liPreQty = int.Parse(string.IsNullOrEmpty(dtAvailable.Rows[i]["Qty"].ToString()) ? "0" : dtAvailable.Rows[i]["Qty"].ToString());
                            if (liPreQty > 15 && !hdndbType.Value.Equals(Utils.AppConstants.VSM))
                                row["Qty"] = 15;
                            else
                                row["Qty"] = liPreQty;
                        }
                        else
                        {
                            liPreQty = int.Parse(string.IsNullOrEmpty(dtAvailable.Rows[i]["Qty"].ToString()) ? "0" : dtAvailable.Rows[i]["Qty"].ToString()) + liPreQty;
                            if (liPreQty > 15 && !hdndbType.Value.Equals(Utils.AppConstants.VSM))
                                row["Qty"] = 15;
                            else
                                row["Qty"] = liPreQty;
                        }
                        dt.Rows.Add(row);
                    }
                }

                if (dt != null)
                {
                    BindBP(dt);
                }
            }
            catch (Exception ex)
            {

            }        
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tb"></param>
        private void BindBP(DataTable tb)
        {
            Session["AvailableTable"] = tb;
            this.grdAva.DataSource = tb;
            this.grdAva.DataBind();
        }

    }
}
